/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/
package pl.vdl.azbest.mremote.probe.signal;

import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/** How to make a queue - study */
public class TestQueue {
	public static void main(String[] args) {
		//TestQueue tq = new TestQueue();
		//tq.startTest();
		LinkedList<String> ll = new LinkedList<String>();  
		ll.add("1st one");
		ll.add("2nd one");
		//System.out.println(ll.getFirst());
		//System.out.println(ll.getLast());
	}

	public void startTest() {
		CmdAdder ca = new CmdAdder();
		CmdRemover cr = new CmdRemover();
		ca.addCommands();
		cr.doCommands();

	}

	private class CmdAdder {
		private int n = 3;

		public void addCommands() {
			final Timer timer = new Timer();
			class MyTimerTask extends TimerTask {
				public void run() {
					// job to do
					Command c = new Cmd_command(n);
					Invoker2.getInstance().addCommand(c);
					//System.out.println("Command no. " + n + " added.");
					// adding timer again
					n--;
					// timer.cancel();
					if (n > 0) {
						// dodajemy co sekunde zadanie do wykonaia.
						timer.schedule(new MyTimerTask(), 1000);
					} else {
						timer.cancel();
					}
				}
			}// MyTimerTask
		}// addCommands
	}

	private class CmdRemover {
		private void doCommands() {
			class Remover implements Runnable {
				public void run() {
					Invoker2.getInstance().invoke();
					Invoker2.getInstance().invoke();
					Invoker2.getInstance().invoke();
				}
			}
			;
			Thread t = new Thread(new Remover());
			t.start();
		}

	}

}

class Invoker2 {
	private static Invoker2 instance = new Invoker2();

	public static Invoker2 getInstance() {
		return instance;
	}

	private final int QUEUE_SIZE = 5;

	private BlockingQueue<Command> bq = new ArrayBlockingQueue<Command>(
			QUEUE_SIZE);

	public void addCommand(Command c) {
		bq.add(c);
	}

	public void invoke() {
		Command c;
		if ((c = bq.poll()) != null)
			c.invoke();
		else
			System.out.println("Queue is eempty.");
	}
}

interface Command {
	public void invoke();
}

class Cmd_command implements Command {
	private int n;

	public Cmd_command(int n) {
		this.n = n;
	}

	public void invoke() {
		//System.out.println("wykonuje zadanie: " + n + " .");
		try {
			Thread.sleep(3 * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//System.out.println("Zadanie wykonane. (" + n + ")");

	}

}
